﻿function open_popup(popurl, width, height, close_popup, hide_clear_button, request_data, popup_mode) {
    window.document.popup_request_data = request_data;
    window.document.close_popup = close_popup;

    var url = popurl;
    if (url.indexOf('?') < 0) {
        url = popurl + "?q=1";
    }
    if (hide_clear_button) {
        url += '&hide_clear_button=true';
    }
    if (popup_mode) {
        url += '&mode='+popup_mode;
    }
    windowName = 'popup_window';
    windowFeatures = 'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,menubar=false,location=false,status=yes';
    var win = window.open(url, windowName, windowFeatures);
    if (window.focus) {
        win.focus();
    }
}
function get_popup_request_data() {
    return window.document.popup_request_data;
}
function get_close_popup() {
    return window.document.close_popup;
}
function set_return(popup_return_data) {

    var form_name = popup_return_data.form_name;
    var name_to_value_array = popup_return_data.name_to_value_array;
    for (var the_key in name_to_value_array) {
        var displayValue = name_to_value_array[the_key].replace(/&amp;/gi, '&').replace(/&lt;/gi, '<').replace(/&gt;/gi, '>').replace(/&#039;/gi, '\'').replace(/&quot;/gi, '"');
        if (window.document.forms[form_name] && window.document.forms[form_name].elements[the_key]) {
            if (window.document.forms[form_name].elements[the_key].tagName == 'SELECT') {
                var selectField = window.document.forms[form_name].elements[the_key];
                for (var i = 0; i < selectField.options.length; i++) {
                    if (selectField.options[i].text == displayValue) {
                        selectField.options[i].selected = true;
                        break;
                    }
                }
            } else {
                window.document.forms[form_name].elements[the_key].value = displayValue;
            }
        }
    }

}
function set_return_and_save_background(popup_reply_data) {
    var form_name = popup_reply_data.form_name;
    var name_to_value_array = popup_reply_data.name_to_value_array;
    var passthru_data = popup_reply_data.passthru_data;
    var query_array = new Array();
    if (name_to_value_array != 'undefined') {
        for (var the_key in name_to_value_array) {
            if (the_key == 'toJSON')
            { }
            else
            { query_array.push(the_key + "=" + name_to_value_array[the_key]); } 
        } 
    }
    var selection_list = popup_reply_data.selection_list;
    if (selection_list != 'undefined') {
        //alert(selection_list);
        for (var the_value in selection_list)
        { query_array.push('subpanel_id=' + selection_list[the_value]); }
    }
    var return_url = passthru_data.return_url;
    if (return_url.indexOf("?") < 0)
        return_url += "?q=1";
    return_url += "&"+query_array.join("&");
    //alert(return_url);


    //request_map[request_id] = passthru_data['child_field'];
    //var returnstuff
    if(renderData)
        $.post(return_url, null, renderData);
    else
        $.post(return_url);

    //got_data(returnstuff, true);
    var refresh_page = escape(passthru_data['refresh_page']);
    if (refresh_page == 1) { document.location.reload(true); }
}
function sendback(associated_row_data) {
    get_request_data();
    var form_name = request_data.form_name;
    var field_to_name_array = request_data.field_to_name_array;
    var call_back_function = eval("window.opener." + request_data.call_back_function);
    //var associated_row_data = associated_javascript_data[id];

    var array_contents = new Array();
    for (var the_key in field_to_name_array) {
        var the_name = field_to_name_array[the_key];
        var the_value = eval("associated_row_data." + the_key.toLowerCase());
        if (typeof (the_value) == 'string') {
            the_value = the_value.replace(/\r\n/g, '\\n');
        }
        array_contents.push('"' + jQuery.trim(the_name) + '":"' + jQuery.trim(the_value) + '"');
    }
    eval("var name_to_value_array = {" + array_contents.join(",") + "}");
    var result_data = { "form_name": form_name, "name_to_value_array": name_to_value_array };
    call_back_function(result_data);
    var close_popup = window.opener.get_close_popup();
    if (close_popup) {
        window.close();
    }
}
function sendbackByRowId(associated_row, id) {
    sendback(associated_row[id]);
}
function sendbackSelected(jqGrid, error_message) {

    var array_contents=jqGrid.getGridParam('selarrrow');

    if (array_contents.length == 0) {
        window.alert(error_message); return;
    }
    eval("var selection_list_array = array_contents");
    eval("var temp_request_data = " + window.document.forms['popup_query_form'].request_data.value);
    
    if (temp_request_data.jsonObject) {
        var request_data = temp_request_data.jsonObject;
    }
    else {
        var request_data = temp_request_data;
    }

    var passthru_data = Object();
    if (typeof (request_data.passthru_data) != 'undefined') {
        passthru_data = request_data.passthru_data;
    }
    
    var form_name = request_data.form_name;
    var field_to_name_array = request_data.field_to_name_array;
    var call_back_function = eval("window.opener." + request_data.call_back_function);
    var result_data = { "form_name": form_name, "selection_list": selection_list_array, "passthru_data": passthru_data };

    var close_popup = window.opener.get_close_popup(); 
    call_back_function(result_data);
    if (close_popup) {
        window.close();
    }
}

function get_request_data() {
    request_data = window.opener.get_popup_request_data();
}